1
|
|
|
import { Inject } from '@nestjs/common'; |
2
|
|
|
import { CommandHandler } from '@nestjs/cqrs'; |
3
|
|
|
import { CreateLeaveRequestCommand } from './CreateLeaveRequestCommand'; |
4
|
|
|
import { ILeaveRequestRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRequestRepository'; |
5
|
|
|
import { LeaveRequest } from 'src/Domain/HumanResource/Leave/LeaveRequest.entity'; |
6
|
|
|
import { DoesLeaveRequestExistForPeriod } from 'src/Domain/HumanResource/Leave/Specification/DoesLeaveRequestExistForPeriod'; |
7
|
|
|
import { LeaveRequestAlreadyExistForThisPeriodException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestAlreadyExistForThisPeriodException'; |
8
|
|
|
import { DoesLeaveExistForPeriod } from 'src/Domain/FairCalendar/Specification/DoesLeaveExistForPeriod'; |
9
|
|
|
import { EventsOrLeavesAlreadyExistForThisPeriodException } from 'src/Domain/FairCalendar/Exception/EventsOrLeavesAlreadyExistForThisPeriodException'; |
10
|
|
|
import { IMattermostNotifier } from 'src/Application/IMattermostNotifier'; |
11
|
|
|
import { ICommandBus } from 'src/Application/ICommandBus'; |
12
|
|
|
import { CreateNotificationCommand } from 'src/Application/Notification/Command/CreateNotificationCommand'; |
13
|
|
|
import { NotificationType } from 'src/Domain/Notification/Notification.entity'; |
14
|
|
|
import { ITranslator } from 'src/Infrastructure/Translations/ITranslator'; |
15
|
|
|
import { ConfigService } from '@nestjs/config'; |
16
|
|
|
|
17
|
|
|
@CommandHandler(CreateLeaveRequestCommand) |
18
|
|
|
export class CreateLeaveRequestCommandHandler { |
19
|
|
|
constructor( |
20
|
|
|
@Inject('ILeaveRequestRepository') |
21
|
|
|
private readonly leaveRequestRepository: ILeaveRequestRepository, |
22
|
|
|
@Inject('ICommandBus') |
23
|
|
|
private readonly commandBus: ICommandBus, |
24
|
|
|
private readonly doesLeaveRequestExistForPeriod: DoesLeaveRequestExistForPeriod, |
25
|
|
|
private readonly doesLeaveExistForPeriod: DoesLeaveExistForPeriod, |
26
|
|
|
@Inject('ITranslator') |
27
|
|
|
private readonly translator: ITranslator, |
28
|
|
|
private readonly configService: ConfigService |
29
|
|
|
) {} |
30
|
|
|
|
31
|
|
|
public async execute(command: CreateLeaveRequestCommand): Promise<string> { |
32
|
|
|
const { |
33
|
|
|
user, |
34
|
|
|
endDate, |
35
|
|
|
endsAllDay, |
36
|
|
|
type, |
37
|
|
|
startDate, |
38
|
|
|
startsAllDay, |
39
|
|
|
comment |
40
|
|
|
} = command; |
41
|
|
|
|
42
|
|
|
if ( |
43
|
|
|
true === |
44
|
|
|
(await this.doesLeaveRequestExistForPeriod.isSatisfiedBy( |
45
|
|
|
user, |
46
|
|
|
startDate, |
47
|
|
|
endDate |
48
|
|
|
)) |
49
|
|
|
) { |
50
|
|
|
throw new LeaveRequestAlreadyExistForThisPeriodException(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ( |
54
|
|
|
true === |
55
|
|
|
(await this.doesLeaveExistForPeriod.isSatisfiedBy( |
56
|
|
|
user, |
57
|
|
|
startDate, |
58
|
|
|
endDate |
59
|
|
|
)) |
60
|
|
|
) { |
61
|
|
|
throw new EventsOrLeavesAlreadyExistForThisPeriodException(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
const leaveRequest = await this.leaveRequestRepository.save( |
65
|
|
|
new LeaveRequest( |
66
|
|
|
user, |
67
|
|
|
type, |
68
|
|
|
startDate, |
69
|
|
|
startsAllDay, |
70
|
|
|
endDate, |
71
|
|
|
endsAllDay, |
72
|
|
|
comment |
73
|
|
|
) |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
this.commandBus.execute( |
77
|
|
|
new CreateNotificationCommand( |
78
|
|
|
NotificationType.POST, |
79
|
|
|
this.translator.translate( |
80
|
|
|
'leave-requests-create-notification-message', |
81
|
|
|
{ |
82
|
|
|
userFirstName: leaveRequest.getUser().getFirstName(), |
83
|
|
|
link: `${this.configService.get<string>( |
84
|
|
|
'PERMACOOP_BASE_URL' |
85
|
|
|
)}/app/people/leaves/${leaveRequest.getId()}` |
86
|
|
|
} |
87
|
|
|
), |
88
|
|
|
leaveRequest |
89
|
|
|
) |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
return leaveRequest.getId(); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|